package com.wx.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.mall.goods.entity.Album;
import com.wx.mall.goods.mapper.AlbumMapper;
import com.wx.mall.goods.service.IAlbumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-15
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public PageInfo<Album> find(Album album, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);

        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(album.getId())){
            queryWrapper.eq("id",album.getId());
        }
        if(!StringUtils.isEmpty(album.getTitle())){
            queryWrapper.like("title",album.getTitle());
        }
        if(!StringUtils.isEmpty(album.getImageItems())){
            queryWrapper.like("image_items",album.getImageItems());
        }
        if("1".equals(album.getImageNum())){
            queryWrapper.orderByDesc("title");
        }else{
            queryWrapper.orderByDesc("sort");
        }

        return new PageInfo<Album>(albumMapper.selectList(queryWrapper));
    }

    @Override
    public List<Album> findAll() {
        return albumMapper.find(new Album());
    }

    @Override
    @Transactional
    public Integer deletes(String ids) {
        String[] idString = ids.split(",");
        int num =0;
        for (String id : idString){
            num+=albumMapper.remove(Integer.valueOf(id));
        }
        return num;
    }

    @Override
    public void save2(Album album) {
        album.setAddTime(new Date());
        albumMapper.save(album);
    }

    @Override
    public void update(Album album) {
        albumMapper.updateData(album);
    }
}
